#include "Time.h"


namespace Cartesia {

LARGE_INTEGER	Time::_performanceCount1;
LARGE_INTEGER	Time::_performanceCount2;
LARGE_INTEGER	Time::_frequency;

double		Time::_timeBetweenFrames = 0;
double		Time::_measureFpsSample = 0;
int			Time::_FPS = 0;
long int	Time::_frameCounter = 0;

uint		Time::_minutes = 0, Time::_seconds = 0;
bool		Time::_pause = false;


Time::Time(void)
{
}

Time::~Time(void)
{
}



void Time::Play()
{	
	_pause = false;

	QueryPerformanceFrequency(&_frequency);
	QueryPerformanceCounter(&_performanceCount1);

	_timeBetweenFrames = 0;
	_measureFpsSample = 0;
	_FPS = 0;
	_frameCounter = 0;
}


void Time::Pause()
{
	_pause = true;
	_timeBetweenFrames = 0;
}


void Time::StartClock(unsigned int minutes, unsigned int seconds)
{
	_minutes = minutes;
	_seconds = seconds;
}



void Time::FirstMeasure()
{
	_minutes = _seconds = 0;
	Play();
}

void Time::Measure()
{
	if(!_pause)
	{
		QueryPerformanceCounter(&_performanceCount2);

		_timeBetweenFrames = (double)((_performanceCount2.QuadPart-_performanceCount1.QuadPart)*1000.0f/_frequency.QuadPart);
		_measureFpsSample += _timeBetweenFrames;

		_frameCounter++;
		_performanceCount1 = _performanceCount2;

		if (_measureFpsSample >= 1000)
		{
			_seconds++;
			if(_seconds >= 60) {
				_seconds = 0;
				_minutes++;
			}

			_measureFpsSample -= 1000;
			_FPS = (short) _frameCounter;
			_frameCounter = 0;
		}
	}
}


} // end namespace